package com.mlt.back.web.servlet;

import com.mlt.back.beans.InfoDetail;
import com.mlt.back.service.InfoDetailService;
import com.mlt.back.service.impl.InfoDetailServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet({"/infoDetail/add","/infoDetail/modify","/infoDetail/remove"})
public class InfoDetailServlet extends HttpServlet {
    private InfoDetailService infoDetailService  = new InfoDetailServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String servletPath = req.getServletPath();
        req.setCharacterEncoding("UTF-8");
        //添加
        if ("/infoDetail/add".equals(servletPath)){
            doInfoDetailAdd(req, resp);
        }
        //修改
        if ("/infoDetail/modify".equals(servletPath)){
            doInfoDetailModify(req,resp);
        }
        //删除
        if ("/infoDetail/remove".equals(servletPath)){
            doInfoDetailRemove(req,resp);
        }
    }
    //添加
    private void doInfoDetailAdd(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        // 获取前端传值
        String fkDetailInfoId = req.getParameter("fk_basic_info_id");
        String infoDetailName = req.getParameter("info_detail_name");
        String infoDetailDesc = req.getParameter("info_detail_desc");

        // 构造实体类
        InfoDetail infoDetail = new InfoDetail(0,infoDetailName,infoDetailDesc,Integer.parseInt(fkDetailInfoId));
        // 执行添加
        Integer add = infoDetailService.add(infoDetail);
        // 处理响应中文
        resp.setContentType("text/html;charset=utf-8");
        // 判断回传
        if (add > 0){
            resp.getWriter().print("<script>alert('添加成功');location.href='/mlt_war_exploded/info_detail_list.jsp'</script>");
        }else {
            resp.getWriter().print("<script>alert('添加失败');location.href='/mlt_war_exploded/add_detail_info.jsp'</script>");
        }

    }


    //修改
    private void doInfoDetailModify(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException{
        // 获取前端传值
        String infoDetailId = req.getParameter("info_detail_id");
        String fkDetailInfoId = req.getParameter("fk_basic_info_id");
        String infoDetailName = req.getParameter("info_detail_name");
        String infoDetailDesc = req.getParameter("info_detail_desc");
        // 构造实体类
        InfoDetail infoDetail = new InfoDetail(Integer.parseInt(infoDetailId),infoDetailName,infoDetailDesc,Integer.parseInt(fkDetailInfoId));
        // 执行更新
        Integer modify = infoDetailService.modify(infoDetail);
        // 处理响应中文
        resp.setContentType("text/html;charset=utf-8");
        // 判断回传
        if (modify > 0){
            resp.getWriter().print("<script>alert('更新成功');location.href='/mlt_war_exploded/info_detail_list.jsp'</script>");
        }else {
            resp.getWriter().print("<script>alert('更新失败');location.href='/mlt_war_exploded/info_detail_list.jsp'</script>");
        }

    }

    //删除
    private void doInfoDetailRemove(HttpServletRequest req, HttpServletResponse resp)
            throws IOException{

        // 获取前端传值
        String id = req.getParameter("id");
        // 执行删除
        Integer remove = infoDetailService.remove(Integer.parseInt(id));
        // 处理响应中文
        resp.setContentType("text/html;charset=utf-8");
        // 判断回传
        if (remove > 0){
            resp.getWriter().print("<script>alert('删除成功');location.href='/mlt_war_exploded/info_detail_list.jsp'</script>");
        }else {
            resp.getWriter().print("<script>alert('删除失败');location.href='/mlt_war_exploded/info_detail_list.jsp'</script>");
        }

    }
}
